/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.produce.data.controller;

import javax.servlet.http.HttpServletRequest;

import me.send.data.service.pojo.Manager;
import me.send.data.service.service.ManagerService;
import me.send.framework.util.LoggerUtils;
import me.send.framework.util.MD5Util;
import me.send.produce.data.controller.common.CommonController;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author maxmin
 *
 * @date 2015年9月11日 上午10:37:41
 */
@Controller
@RequestMapping()
public class HomeController extends CommonController {
	
	@Autowired
	ManagerService managerService;
	
	@RequestMapping(value="login")
	public String login(HttpServletRequest req,Model model){
		LoggerUtils.info(getClass().getName(), "visit into loginController");
		if (SecurityUtils.getSubject().isAuthenticated()){
            LoggerUtils.info(getClass().getName(),"已登录过，直接跳转到用户中心");
            return "redirect:main/stage/main";
        }
		String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        LoggerUtils.info(getClass().getName(),"visit into login method!");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户不存在";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "密码不正确";
        } else if (LockedAccountException.class.getName().equals(exceptionClassName)) {
			error="用户被禁用";
		} else if (ExcessiveAttemptsException.class.getName().equals(exceptionClassName)) {
			error="请求次数过多，用户被锁定";
		}else if ("JCaptchaError".equals(exceptionClassName)) {
        	error = "验证码错误";
		} else if(exceptionClassName!=null) {
            error = "未知错误，无法完成登录";
        } 
        model.addAttribute("error", error);
		return "login";
	}
	
	@RequestMapping(value="forget")
	public ModelAndView withdraw_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"forget");
		return modelAndView;
	}
	
	@RequestMapping(value="confirm",method= RequestMethod.POST)
	public String confirm(HttpServletRequest request,String username,String password,String newpassword1,String newpassword2) {
		if(newpassword1==null||newpassword2==null||username==null||password==null){
			return "redirect:/forget";
		}
		if("".equals(username.trim())||"".equals(password.trim())||"".equals(newpassword1.trim())||"".equals(newpassword2.trim())){
			return "redirect:/forget";
		}
		if(!newpassword1.equals(newpassword2)){
			return "redirect:/forget";
		}
		Manager manager = managerService.getByTelephone(username);
		if(manager==null){
			return "redirect:/forget";
		}
		
		managerService.update(username, MD5Util.MD5(password), MD5Util.MD5(newpassword1));
		return "redirect:/login";
	}
	
	/**
     * 退出登录状态
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "redirect:/login";
    }
    
    @RequestMapping(value="/")
    public String index(){
    	return "index";
    }
    
    public static void main(String[] args) {
		
	}

}
